// pages/goods-list/goods-list.js
var t = require("../../api.js"),app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuList: [],
    subMenuList: [],
    initId: '',
    initPid: '',
    goodsList: [],
    selected: '默认',
    selectedIndex: 0,
    showOptions: false,
    cartNumber:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.id) {
      this.setData({
        initId:options.id
      })
    };
    if (options.pid) {
      this.setData({
        initPid:options.pid
      })
      // this.getSubMenu(options.pid);
      app.request({
        url: t.default.cate + '&type=2&parent_id=' + options.pid,
        method: "get",
        success: (result) => {
          console.log(result)
          if (result.data.length) {
            this.setData({
              subMenuList: result.data,
              initId: this.options.id
            })
            this.getGoodsList(this.options.id)
          } else {
            this.setData({
              goodsList: []
            })
          }
        }
      })
    };
    app.request({
      url: t.default.cate + '&type=1',
      method: "get",
      success: (result) => {
        console.log(result);
        this.setData({
          menuList: result.data
        });
      }
    })
  },
  //显示排序
  showSelect() {
    if (this.data.showOptions) {
      this.setData({
        showOptions: false
      })
    } else {
      this.setData({
        showOptions: true
      })
    }
  },
  //切换排序
  changeOption(index) {
    var index = Number(index.currentTarget.dataset.i);
    console.log(index)

    switch (index) {
      case 0:
        this.setData({
        selected: '默认',
        selectedIndex:0
      })
      break;
      case 1:
        this.setData({
        selected: '时间',
        selectedIndex:1
      })
      break;
      case 2:
        this.setData({
        selected: '价格',
        selectedIndex:2
      })
      break;
      case 3:
        this.setData({
        selected: '销量',
        selectedIndex:3
      })
      break;
    };
    this.setData({
      showOptions: false
    })
  },
  //开始搜索
  onSearch(options) {
    this.setData({
      initId: '',
      initPid: '',
      subMenuList: []
    })
    console.log(options.detail.value)
    app.request({
      url: t.default.goods_list + '&sort=' + this.data.selectedIndex + '&keyword' + options.detail.value,
      method: "get",
      success: (result) => {
        this.setData({
          goodsList: result.data.list
        })
        console.log(result)
      }
    })
  },
  // 点击一级菜单
  navMenu: function (current) {
    console.log(current)
    this.setData({
      initPid: current.currentTarget.dataset.id,
      subMenuList: []
    })
    this.getSubMenu(current.currentTarget.dataset.id);
  },
  // 获取子菜单列表
  getSubMenu: function (id) {
    app.request({
      url: t.default.cate + '&type=2&parent_id=' + id,
      method: "get",
      success: (result) => {
        console.log(result)
        if (result.data.length) {
          this.setData({
            subMenuList: result.data,
            initId: result.data[0].id
          })
          this.getGoodsList(result.data[0].id)
        } else {
          this.setData({
            goodsList: []
          })
        }
      }
    })
  },
  // 点击子菜单列表
  getGoodsList(ids) {
    var id = ids.currentTarget && ids.currentTarget.dataset && ids.currentTarget.dataset.id || ids
    this.setData({
      initId: id
    })
    app.request({
      url: t.default.goods_list + '&cat_id=' + id,
      method: "get",
      success: (result) => {
        this.setData({
          goodsList: result.data.list
        })
        console.log(result)
      }
    })
  },
  //进入商品详情
  toGoods(options) {
    wx.navigateTo({url:"/pages/goods/goods?id="+options.currentTarget.dataset.id+"&handle=true"})
  },
  //去购物车
  toCart(options) {
    wx.navigateTo({url:"/pages/cart/cart"})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.request({
      url: t.cart.list,
      method: "get",
      success: (result) => {
        console.log(result, '---------------------------------------------')
        if (result.code == 0) {
          this.setData({
            cartNumber: result.data.num_total
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})